package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.deyinet.bdcms.core.vo.Page;
import com.deyinet.bdcms.entity.Deviceowner;
import com.deyinet.bdcms.entity.Syslog;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.service.IDeviceownerService;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.service.ISysmoduleService;
import com.deyinet.bdcms.tools.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Bacel on 2017/4/6.
 */
@Controller
@RequestMapping(value = "/sysmanage/customer", produces = "text/html;charset=UTF-8")
public class CustomerController {

    @Autowired
    private IDeviceownerService moduleService;
    @Autowired
    private ISysmoduleService sysmoduleService;
    @Autowired
    private ILogsService logsService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public void list() {
    }

    @RequestMapping(value = "/add")
    public void add(String id, Model model) {
        model.addAttribute("id", id);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody Deviceowner module, HttpServletRequest request) {
        Map<String, Object> info = new HashMap<>();
        Syslog log = new Syslog();
        info.put("content", "操作失败");
        if (module != null) {
            if (module.getCreatedate() == null)
                module.setCreatedate(new Timestamp(System.currentTimeMillis()));
            if (module.getId() != null) {
                module.setCreatedate(new Timestamp(System.currentTimeMillis()));
                Deviceowner old = moduleService.get(module.getId());
                old.setName(module.getName());
                old.setContact(module.getContact());
                old.setTel(module.getTel());
                old.setLocation(module.getLocation());
                 moduleService.updateT(old);
                log.setTitle("修改设备数据");
                info.put("content", "修改成功");
                log.setContent("修改id为"+old.getId()+"的负责人");
            } else {
                moduleService.insertT(module);
                log.setTitle("添加设备数据");
                info.put("content", "添加成功");
                log.setContent("添加id为"+module.getId()+"的负责人");
            }}
            Sysmodule sysmodule = new Sysmodule();
            sysmodule.setId(15);
            log.setSysmoduleByModuleid(sysmodule);
            log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
            log.setCreatedate(new Timestamp(System.currentTimeMillis()));
            WebUtilify.WriteLog(request,log, logsService);
            info.put("Message", "ok");
            return info;
        }

    @RequestMapping(value = "/listdata")
    @ResponseBody
    public Map<String, Object> listdata(final HttpServletRequest request, Page<Deviceowner> page) {
        String customerName = request.getParameter("customerName");
        String contact= request.getParameter("contact");
        Deviceowner deviceowner = new Deviceowner();
        deviceowner.setName(customerName);
        deviceowner.setTel(contact);
        if (customerName != null && !"".equals(customerName)) {
            deviceowner.setName(customerName);
        }
        if (contact != null && !"".equals(contact)) {
            deviceowner.setTel(contact);
        }
        moduleService.getPageByExampleLike(deviceowner, page);
        Map<String, Object> info = new HashMap<>();
        info.put("totalRow", page.getTotalRow());
        info.put("pageCurrent", page.getPageCurrent());
        info.put("list", page.getList());
        return info;
    }


    @RequestMapping(value = "/customerList")
    @ResponseBody
    public List<Deviceowner> customerList() {
        return moduleService.getList();
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(String json, HttpServletRequest request) {
        Syslog log = new Syslog();
        List<Deviceowner> devices = JSONArray.parseArray(json, Deviceowner.class);
        Integer[] idarr = new Integer[devices.size()];
        String content="";

        for (int i = 0; i < devices.size(); i++) {
            idarr[i] = devices.get(i).getId();
            content+=idarr[i];
            if (i!=devices.size()-1){
                content+=",";
            }
        }
        moduleService.batchDelete(idarr);
        log.setTitle("删除客户信息");
        Sysmodule sysmodule = new Sysmodule();
        sysmodule.setId(15);
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setContent("删除id为"+content+"的负责人");
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        Map<String, Object> info = new HashMap<>();
        info.put("message", "删除成功");
        info.put("status", 200);
        info.put("closeCurrent", "false");
        return info;
    }
    @RequestMapping(value = "/getmodule")
    @ResponseBody
    public Deviceowner getmodule(int id) {
        return moduleService.get(id);
    }

   }
